package hui.com.service.impl;

import hui.com.dao.SalesDao;
import hui.com.model.entity.sales.SalesEntity;
import hui.com.model.req.sales.RequestGetLikeSales;
import hui.com.model.req.sales.RequestGetSalesForecast;
import hui.com.model.res.sales.ResponseGetSale;
import hui.com.model.res.sales.ResponseGetSaleList;
import hui.com.model.res.sales.ResponseGetSalesForecastList;
import hui.com.service.SalesService;
import hui.com.util.DateUtil;
import hui.com.util.IntegerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

@Service
public class SalesServiceImpl implements SalesService {
    @Resource
    SalesDao salesDao;

    @Override
    public Boolean salesADD(SalesEntity sales) {
        return salesDao.salesADD(sales);
    }

    @Override
    public String salesADD_getCode() {
        RequestGetLikeSales sales = new RequestGetLikeSales();
        SalesEntity saleCode = new SalesEntity();

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前月第一天的最小时间
        LocalDateTime firstDayOfMonth =  LocalDateTime.of(currentDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
        // 获取当前月的最大时间（年月日秒）
        LocalDateTime lastDayOfMonth = LocalDateTime.of(currentDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
        sales.setStrTime(firstDayOfMonth);
        sales.setEndTime(lastDayOfMonth);
        Integer size =  salesDao.getListSalesPage(sales);

        size = (size==null || size==0)?1:size;
//        out.println(size);
        String salesCode = null;

        do{

            String salesCodes = IntegerUtil.getBH(size,4);

            salesCodes ="SALES-" + DateUtil.currentYearMontID() + "-" + salesCodes;

            saleCode.setSalesCode(salesCodes);

            salesCode = salesCodes;

            size++;

//            out.println(salesCodes);
//            out.println(salesDao.getListSalesPage(salesEntityCode,null,null));
//            out.println(salesDao.getListSalesPage(salesEntityCode,null,null)<=0);
        }while (salesDao.getSales(saleCode)!=null);


        return salesCode;
    }

    @Override
    public Integer salesCount() {
        return salesDao.salesCount();
    }

    @Override
    public SalesEntity getSales(SalesEntity sales) {
        return salesDao.getSales(sales);
    }

    @Override
    public ResponseGetSaleList getListSales(RequestGetLikeSales sales) {
        ResponseGetSaleList salesRespone = new ResponseGetSaleList();
        int currentPage = sales.getCurrentPage()!=null?sales.getCurrentPage():0;

        int pageSize =( sales.getPageSize()!=null && sales.getPageSize()!=0)?sales.getPageSize():30;
        Integer currentIndex = currentPage-1>=0?((currentPage-1)*pageSize):pageSize;

        sales.setCurrentIndex(currentIndex);
        sales.setPageSize(pageSize);
        salesRespone.setSalesList(salesDao.getListSales(sales));
        salesRespone.setPageSizeMax(salesDao.getListSalesPage(sales));
        return salesRespone;
    }

    @Override
    public Integer getListSalesPage(RequestGetLikeSales sales) {
        return salesDao.getListSalesPage(sales);
    }

    @Override
    public List<ResponseGetSale> salesADD_getSalesForecastList(RequestGetLikeSales sales) {
        return salesDao.salesADD_getSalesForecastList(sales);
    }

    @Override
    public Boolean updateSales(SalesEntity sales) {
        return salesDao.updateSales(sales);
    }

    @Override
    public Boolean salesHistoryADD(SalesEntity sales) {
        return salesDao.salesHistoryADD(sales);
    }

    @Override
    public List<ResponseGetSale> getSalesHistoryList(SalesEntity sale) {
        return salesDao.getSalesHistoryList(sale);
    }
}
